package com.htdata.iiot.api.tsdb.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chensi
 */
public class Tools {
    /**
     * average a list into small list
     * @param allList
     * @param threadNum
     * @return
     */
    public static <T> List<T>[] avgList(List<T> allList, int threadNum){
        List<T>[] listA = new List[threadNum];
        for(int i=0;i<listA.length;i++){
            listA[i] = new ArrayList();
        }
        for(int i=0;i<allList.size();i++){
            listA[i%threadNum].add(allList.get(i));
        }
        return listA;
    }

    /**
     * 计算起始时间至结束时间中间的小时
     * egg:
     *  start="2018-06-13 08:13:13" end="2018-06-13 10:13:13"
     *  return ["2018-06-13 08:00:00","2018-06-13 09:00:00","2018-06-13 10:00:00"]
     * @param start
     * @param end
     * @return
     * @throws ParseException
     */
    public static String[] splitDate(String start,String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long startL = sdf.parse(start).getTime();
        long endL = sdf.parse(end).getTime();
        long tmp = endL;
        List<String> list = new ArrayList<>();
        while (tmp>=startL){
            list.add(sdf2.format(new Date(tmp)));
            tmp = tmp - 60*60*1000;
        }
        String[] r = new String[list.size()];
        list.toArray(r);
        return r;
    }

    public static byte[] combineArray(byte[]... arrays){
        int length = 0;
        for(byte[] a:arrays){
            if(a==null){
                return null;
            }
            length += a.length;
        }
        byte[] r = new byte[length];
        int index = 0;
        for(byte[] a:arrays){
            for (byte b:a){
                r[index] = b;
                index++;
            }
        }
        return r;
    }

}
